package com.xd.case2;

import org.apache.zookeeper.KeeperException;

import java.io.IOException;

public class DistributeTest {

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        // 创建分布式锁 1
        DistributeLock lock1 = new DistributeLock();
        // 创建分布式锁 2
        DistributeLock lock2 = new DistributeLock();

        new Thread(new Runnable() {
            @Override
            public void run() {
                //获取锁对象
                try {
                    lock1.zkLock();
                    System.out.println("线程1的锁获取了");
                    Thread.sleep(5 * 100);
                    lock1.unZkLock();
                    System.out.println("线程1释放锁");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                //获取锁对象
                try {
                    lock2.zkLock();
                    System.out.println("线程2的锁获取了");
                    Thread.sleep(5 * 100);
                    lock2.unZkLock();
                    System.out.println("线程2释放锁");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
